package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Category;
import com.itheima.common.PageParm;
import com.itheima.common.R;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;



    @GetMapping("/page")
    public R selectPage(PageParm parm){
        IPage<Category> categoryIPage = categoryService.selectPage(parm);
        return R.success(categoryIPage);
    }

    @PostMapping
    public R AddCategory(@RequestBody Category category){
        int row = categoryService.insert(category);
        if(row>0)
            return R.success("添加成功！！！");
        else return R.error("添加失败！！！");

    }

    @PutMapping
    public R updateCategory( @RequestBody Category category){
        int row = categoryService.updateById(category);
        if(row>0)
            return R.success("修改成功!!!!");
        else return R.error("修改失败！！！");

    }

    @DeleteMapping
    public  R deleteById( @RequestBody Category category){
        int delete = categoryService.delete(category);
        if(delete>0){
            return R.success("删除成功！！！");
        }
        else return R.error("删除失败！！！");

    }

    @GetMapping("/list")
    public R selectCategoryByType(int type){
        List<Category> categories = categoryService.selectByType(type);

        return R.success(categories);

    }

    @GetMapping("/Alllist")
    public R selectAllCategory(){
        List<Category> categories = categoryService.selectAll();
        return R.success(categories);

    }


}
